<template>
  <div class="page-login">
    <div class="page-login-banner flex-1 flex-col align-items-center justify-center"> 
      <div style="width: 100%">
        <img src="@/assets/loginbg.png" style="object-fit:cover; width: 100%"/>
      </div>
    </div>
    <div class="page-login-logincontent flex-1 flex-col align-items-center justify-center">
      <div class="page-login-loginwarp">
        <div class="page-login-logintitle f-24 bold text-center">督考测评系统</div>
        <div class="page-login-logininfo mt-24">
          <el-form
            :label-position="labelPosition"
            label-width="auto"
            :rules="rules"
            :model="ruleForm"
            ref="ruleFormRef"
          >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"  size="large"  placeholder="请输入用户名" :prefix-icon="User"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" size="large" show-password placeholder="请输入密码" :prefix-icon="Lock"/>
          </el-form-item>  
          </el-form>

          <div class="mt-24 mb-24">
            <el-button type="primary" size="large" style="width: 100%;" @click="submitForm(ruleFormRef)">登 录</el-button>
          </div>
        </div>
        <div class="page-login-agreement mt-48 flex-row justify-center mb-30 f-14 gray-color">
          <span>阅读并接受 </span><span>《服务条款》</span><span>《隐私政策》</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { Login } from '@/api/api'
import { setKeyItem } from '@/utils/auth'
import { AES_Encrypt } from '@/utils/3des'
const router = useRouter()
const labelPosition = ref('top')
const ruleFormRef = ref(null)
const ruleForm = ref({
  username: '',
  password: ''
})

const rules = reactive({
  username: [
    { required: true, message: '请输入用户', trigger: 'blur' },
    { required: true, message: '请输入用户', trigger: 'change' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { required: true, message: '请输入密码', trigger: 'change' },
  ]
})


const setLogin = () => {
  Login({
    ...ruleForm.value,
    password: AES_Encrypt(ruleForm.value.password)
  }).then(res => {
    setKeyItem('token',  res.resData.token)
    router.push({ path: '/admin/user' })
  })
}

const submitForm = async () => {
  await  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      setLogin() 
    } else {
      console.log('error submit!!');
      return false;
    }
  })
}


onMounted(() => {
  document.onkeydown = function (e) { // 回车提交表单
  // 兼容FF和IE和Opera
  var theEvent = window.event || e;
  var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code == 13) {
      submitForm()
    }
  }
})

</script>

<style lang="scss" scoped>
.page-login{
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #fff;
  display: flex;
  flex-direction: row;
  &-banner{
    background: url("@/assets/bg-login.png") center center no-repeat;
    // background: url("@/assets/loginbg.png") center center no-repeat;
    background-size: 100% 100%;
  }
  &-logincontent{
    background: #f3f3f3;
    min-width: 100px;
  }
  &-loginwarp{
    width: 400px;
    background: #fff;
    padding: 0 30px;
    border-radius: 12px;
    box-shadow: 0 2px 6px 0 rgba(0, 0, 0, .1);
  }
  &-logintitle{
    padding: 48px 0 0;
    color: #141414;
    font-size: 24px;
    line-height: 32px;
    text-align: center;
  }
}
</style>